<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/10
 * Time: 上午10:08
 */

namespace App\Busi\Modules\Resource;


use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\CompanyTrait;
use App\Busi\Traits\ConsultTrait;
use App\Busi\Traits\PolicyTrait;
use App\Busi\Traits\UserTrait;
use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Http\Requests\UserCompanyInfoRo;
use App\Models\Company;

class UserCompanyInfoBusi extends BaseBusi
{
    use UserTrait,ConsultTrait,PolicyTrait;
    public function handle(UserCompanyInfoRo $ro){
        $uid = $ro->uid;
        $user = $this->getUserByUid($uid);
        if(!$user){
            throw new UserException(UserError::USER_NOT_EXIST);
        }
        $userId = $user['id'];
        $raw_company = $this->getCompanyInfoByUid($uid);
        if(!$raw_company){
            throw new UserException(UserError::USER_NOT_AUTH);
        }
        $orders = $this->getTotalOrdersByUid($uid);
        $ret['company_name'] = $raw_company['name'];
        $ret['account'] = $raw_company['account'];
        if($raw_company['flag']){
            switch ($raw_company['flag']){
                case 0:$ret['status'] = "未认证";break;
                case 1:$ret['status'] = "认证中";break;
                case 2:$ret['status'] = "未通过";break;
                case 3:$ret['status'] = "已认证";break;
                default :$ret['status'] = "未认证";break;

            }
        }

        $ret['star'] = $raw_company['star'];
        $ret['view_num'] = $raw_company['view_num'];
        if($raw_company['logo']){
            $ret['logo'] = config("admin.upload.host").$raw_company['logo'];
        }
        $ret['total_orders'] =$orders['total'];
        $ret['used_orders'] =$orders['used'];
        $ret['unused_orders'] =$orders['balance'];
        $ret['amount'] = $this->getUserAllAmountByUserId($userId);
        return $ret;


    }
    function getCompanyInfoByUid($uid)
    {
        $where = [
            ['uid','=',$uid]
        ];
        $company = Company::where($where)->first();
        return $company;
    }

}